using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [Header("角色设置")]
    [SerializeField] private float moveSpeed = 5f;
    [SerializeField] private Transform attackPoint;
    [SerializeField] private float attackRadius = 0.5f;
    [SerializeField] private LayerMask noteLayer;
    
    [Header("攻击类型")]
    [SerializeField] private float attackCooldown = 0.2f;
    
    // 动画控制
    private Animator animator;
    private float lastAttackTime;
    
    // 攻击状态
    private enum AttackType { High, Mid, Low }
    private AttackType currentAttackType = AttackType.Mid;
    
    // 组件引用
    private Rigidbody2D rb;
    
    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }
    
    private void Update()
    {
        HandleInput();
    }
    
    private void HandleInput()
    {
        // 移动控制
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
        
        // 设置攻击类型
        if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
        {
            currentAttackType = AttackType.High;
        }
        else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
        {
            currentAttackType = AttackType.Low;
        }
        else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
        {
            currentAttackType = AttackType.Mid;
        }
        
        // 攻击控制
        if (Input.GetKeyDown(KeyCode.Space) && Time.time >= lastAttackTime + attackCooldown)
        {
            Attack();
        }
        
        // 移动角色
        Vector2 movement = new Vector2(horizontalInput, verticalInput).normalized * moveSpeed;
        rb.velocity = movement;
        
        // 更新动画参数
        animator?.SetFloat("Speed", movement.magnitude);
    }
    
    private void Attack()
    {
        lastAttackTime = Time.time;
        
        // 播放对应的攻击动画
        switch (currentAttackType)
        {
            case AttackType.High:
                animator?.SetTrigger("AttackHigh");
                break;
            case AttackType.Mid:
                animator?.SetTrigger("AttackMid");
                break;
            case AttackType.Low:
                animator?.SetTrigger("AttackLow");
                break;
        }
        
        // 检测碰撞的音符
        Collider2D[] hitNotes = Physics2D.OverlapCircleAll(attackPoint.position, attackRadius, noteLayer);
        
        foreach (Collider2D note in hitNotes)
        {
            // 获取音符组件并调用命中方法
            Note noteComponent = note.GetComponent<Note>();
            if (noteComponent != null)
            {
                bool isCorrectAttack = CheckAttackType(noteComponent, currentAttackType);
                noteComponent.OnHit(isCorrectAttack);
            }
        }
    }
    
    private bool CheckAttackType(Note note, AttackType attackType)
    {
        // 检查攻击类型是否匹配音符类型
        // 这里需要根据Note类的实现来完善
        return true; // 临时返回值，后续完善
    }
    
    private void OnDrawGizmosSelected()
    {
        // 在编辑器中可视化攻击范围
        if (attackPoint != null)
        {
            Gizmos.color = Color.red;
            Gizmos.DrawWireSphere(attackPoint.position, attackRadius);
        }
    }
}